#import <UIKit/UIKit.h>

@interface UIImage (Resize)

- (UIImage *)resizedImageWithSize:(CGSize)newSize;

@end

@implementation UIImage (Resize)

- (UIImage *)resizedImageWithSize:(CGSize)newSize {
    // 计算缩放比例
    CGFloat scaleFactor = newSize.width / self.size.width;

    // 创建一个新的矩形，其尺寸是缩放后的尺寸
    CGSize newSizeForScale = CGSizeMake(self.size.width * scaleFactor, self.size.height * scaleFactor);
    CGRect newRect = CGRectMake(0, 0, newSizeForScale.width, newSizeForScale.height);

    // 使用Core Graphics创建一个新的图像
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [self drawInRect:newRect];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

@end